package com.jwdf.uda.ui.fragment.find.index;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.ReplyDetailEvent;
import com.jwdf.uda.event.ReplyDetailItemClickEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.QuizIndexAction;
import com.jwdf.uda.ui.adapter.GenericQuizAdapter;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import com.jwdf.uda.utils.EventBusUtil;

import java.util.List;

import retrofit2.Response;
/**
 * Created by fatsfan on 2016/10/9.
 */
@SuppressLint("ValidFragment")
public class FindIndexFragment extends SimpleListFragment {

    protected final int CODE_DRESS = 1, CODE_MAKEUP = 2, CODE_LIFE = 3, CODE_SHOPPING = 4, CODE_EMOTION = 5;
    protected Activity aty;
    protected View layEmpty;
    protected int code;
    private GenericQuizAdapter mAdapter;
    @Override
    protected void initViews() {
        mAdapter = new GenericQuizAdapter(getActivity());
        lv.setAdapter(mAdapter);
        layEmpty = mDataBinding.tvEmptyData;
        aty = getActivity();
        EventBusUtil.registerEvent(this);

    }
    @Override
    protected void onListItemClick(int position) {

    }
    @Override
    protected void requestData(final boolean refresh) {

        if(refresh && mAdapter.isEmpty()) {
            showProgressDialog();
        }

        int offset = refresh ? 0 : mAdapter.getCount();
        onNewRequestCall(QuizIndexAction.newInstance(getContext(), offset, code).request(new AHttpService.IResCallback<PbBean.PbResQuestionQuestionList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResQuestionQuestionList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(getView() != null) {
                    closeProgressDialog();
                    if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        mDataBinding.commonFragmentList.stopRefresh();
                        mDataBinding.commonFragmentList.stopLoadMore();
                        return;
                    }
                    doSuccessResponse(refresh, response.body());
                }
            }
        }));
    }
    private void doSuccessResponse(boolean refresh, PbBean.PbResQuestionQuestionList body) {
        if(body != null) {
            final List<PbBean.PbBaseQuestion> questionInfoList = body.getQuestionInfoList();
            if(refresh) {
                mAdapter.setData(questionInfoList);
            } else {
                mAdapter.append(questionInfoList);
            }

            if(questionInfoList.size() == 0) {
                mDataBinding.commonFragmentList.setEmptyView(layEmpty);
            } else {
                StringBuilder strBoss = new StringBuilder();
                for(PbBean.PbBaseQuestion item : questionInfoList) {
                    final String actionStr = item.getActionStr();
                    if(actionStr != null && actionStr.length() != 0)
                        strBoss.append(actionStr);
                }
                if(strBoss.length() == 0)
                    mDataBinding.tvEmptyData.setText("你关注的人暂时还没有相关操作哦~");
            }

            mDataBinding.commonFragmentList.setPullLoadEnable(questionInfoList.size() == getPageLength());
            mDataBinding.commonFragmentList.stopRefresh();
            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }
    @Override
    public void onDestroyView() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroyView();
    }
    public void onEventMainThread(ReplyDetailEvent event) {
//        if(ReplyDetailEvent.EVENT_DO_REPLY_PRAISE == event.getEvent()) {
//            requestData(true);
//        }
    }
    public void onEventMainThread(ReplyDetailItemClickEvent event) {
        if(ReplyDetailItemClickEvent.EVENT_COMMENT_LIKE_CLICK == event.getEvent()) {
            requestData(true);
        }
    }
}
